﻿/******************************************************************************
 *  作者：       Maxzhang1985
 *  创建时间：   2012/4/12 14:08:30
 *
 *
 ******************************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ExtendPropertyLib.WPF;
using ExtendPropertyLib;
using Tk=Telerik.Windows.Controls;
using System.Windows;
using System.Windows.Controls.Primitives;
using System.Windows.Interop;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Windows.Media;
using System.Windows.Controls;


namespace ExORMDesginer.RadWindow.Helper
{

    /// <summary>
    /// WPF窗口管理器实例
    /// </summary>
    public class RadWindowManager : IWindowManager
    {
   
      
      
        public void Show(ViewModelBase element)
        {
            ShowDialog(element);
        }

        public bool? ShowDialog(ViewModelBase element)
        {
            ViewLocator vl = new ViewLocator();
            var view = vl.FindView(element);
            ViewModelBinder.Bind(element, view, true);
            Tk.RadWindow window = null;
         
            window = new Tk.RadWindow();
            window.Foreground = Brushes.White;
            window.Content = view;

            window.SizeToContent = true;

         
            string title = element.GetViewTitle();
            if (!string.IsNullOrEmpty(title))
            {
                var text = new TextBlock();
                text.Foreground = Brushes.White;
                text.FontSize = 13;
                text.Text = title;
                window.Header = text ;
            }

           window.Loaded += (s, e) => element.OnLoad();

            window.Closed += (s, e) => element.Closed();
          
            window.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            window.ShowDialog();
            return true;
        }

        public void ShowPopup(ViewModelBase element)
        {
            var type = element.GetType();
            ViewLocator vl = new ViewLocator();
            var view = vl.FindView(element);
            ViewModelBinder.Bind(element, view, true);
            var popup = new Popup();

            popup.Placement = PlacementMode.MousePoint;
            popup.AllowsTransparency = true;

            if (view is Window)
            {
                var winView = view as Window;
                var ui = winView.Content as UIElement;
                winView.Content = null;
                popup.Child = ui;
            }
            else
            {
                popup.Child = view;
            }

            popup.IsOpen = true;
            popup.CaptureMouse();
        }

        public void ShowMessage(string msg)
        {
            var text = new TextBlock();
            text.Foreground = Brushes.White;
            text.FontSize = 13;
            text.Text = msg;
            Tk.RadWindow.Alert(text);
        }


        public bool ShowConfirm(string confirmText)
        {
            bool result = false;
            var text = new TextBlock();
            text.Foreground = Brushes.White;
            text.FontSize = 13;
            text.Text = confirmText;
            Tk.RadWindow.Confirm(text, new EventHandler<Tk.WindowClosedEventArgs>((s, e) =>
            {
                result = e.DialogResult.Value;
            }));
            return result;
        }

        public string Prompt(string promptText)
        {
            string result =string.Empty;
            var text = new TextBlock();
            text.Foreground = Brushes.White;
            text.FontSize = 13;
            text.Text = promptText;
            Tk.RadWindow.Prompt(text,new EventHandler<Tk.WindowClosedEventArgs>((s, e) =>
            {
                if (e.PromptResult != null && e.PromptResult != string.Empty)
                {
                    result = e.PromptResult;
                }

            }));
            return result;
        }




        public Size Size
        {
            set;
            get;
        }

        public bool MaxSize
        {
            set;
            get;
        }
    }
}
